<?php

// Prevent direct access to files
define('NBWI', true);

// Root to the nbwi folder
define('NBWI_ROOT', dirname(__FILE__) . '/');

// Exception handler
require_once NBWI_ROOT . 'nbwiExceptionHandler.php';

set_exception_handler('nbwiExceptionHandler');

// Load exception class
require_once NBWI_ROOT . 'nbwiException.php';

// Base extension class
require_once NBWI_ROOT . 'extension.php';

class nbwi
{
	private
		$dbHost		= '',
		$dbUser		= '',
		$dbPass		= '',
		$dbDatabase	= '',
		$dbPrefix	= '',
		$forumPath	= '';
	
	public function __construct($forumPath = '', $sortMagicQuotes = true)
	{
		if ($forumPath == '')
		{
			$forumPath = dirname(NBWI_ROOT);
		}
		
		if ($sortMagicQuotes == true)
		{
			$this->sortMagicQuotes();
		}
		
		// Add trailing slash if it doesn't yet have one
		if (substr($forumPath, -1) != '/')
		{
			$forumPath .= '/';
		}
		
		// Get config values
		list(
			$this->dbHost, $this->dbUser, $this->dbPass, $this->dbDatabase, $this->dbPrefix
		) = $this->loadConfig($forumPath);
	}
	
	public function __call($extension, $args = array())
	{
		$extension = $this->__get($extension);
		
		if (!empty($args))
		{
			call_user_func_array(array($extension, 'load'), $args);
		}
		
		return $extension;
	}
	
	public function __get($extension)
	{
		// Check if the extension exists
		if (file_exists(NBWI_ROOT . 'extensions/' . $extension . '.php'))
		{
			require_once NBWI_ROOT . 'extensions/' . $extension . '.php';
			
			$ext = new $extension(
				$this->dbHost, $this->dbUser, $this->dbPass, $this->dbDatabase, $this->dbPrefix
			);
			
			return $ext;
		}
		else
		{
			throw new nbwiException('The extension \'' . $extension . '\' could not be found', nbwiException::EXT_ERROR);
		}
	}
	
	private function cleanInputData($var)
	{
		if (is_array($var))
		{
			$array = array();
			
			foreach ($var as $key => $value)
			{
				$array[$this->cleanInputKey($key)] = $this->cleanInputData($value);
			}
			
			return $array;
		}
		
		// Remove added slashes for consistency
		if (get_magic_quotes_gpc())
		{
			$var = stripslashes($var);
		}
		
		// Consistent newlines as well
		$var = str_replace(array("\r\n", "\r"), "\n", $var);
		
		return $var;
	}
	
	private function cleanInputKey($key)
	{
		// Only match allowed characters
		if (!preg_match('#^[\w\d:/-]+$#', $key))
		{
			throw new nbwiException('Bad Input', nbwiException::STARTUP_ERROR);
		}
		
		return $key;
	}
	
	private function loadConfig($path)
	{
		if (!file_exists($path . 'includes/config.php'))
		{
			throw new nbwiException('The novaboard config file could not be located.', nbwiException::STARTUP_ERROR);
		}
		
		// Including the file will connect straight to the db, we don't want to do that.
		// Instead, we'll have to load the file's contents and pick out the values.
		$contents = file_get_contents($path . 'includes/config.php');
		
		// Build the expression
		$regex = '#';
		$regex .= '\$mysql_host[\s]+= "(.+?)";[\s]+';
		$regex .= '\$mysql_login[\s]+= "(.+?)";[\s]+';
		$regex .= '\$mysql_pass[\s]+= "(.*?)";[\s]+';
		$regex .= '\$mysql_database[\s]+= "(.+?)";[\s]+';
		$regex .= '\$db_prefix[\s]+= "(.*?)";';
		$regex .= '#s';
		
		preg_match($regex, $contents, $dbInfo);
		
		return array($dbInfo[1], $dbInfo[2], $dbInfo[3], $dbInfo[4], $dbInfo[5]);
	}
	
	private function sortMagicQuotes()
	{
		ini_set('magic_quotes_runtime', 0);
		
		$_GET		= $this->cleanInputData($_GET);
		$_POST		= $this->cleanInputData($_POST);
		$_COOKIE	= $this->cleanInputData($_COOKIE);
	}
}

?>